//
//  SLHomeSlidMenuView.m
//  SlockApp
//
//  Created by 凯朱 on 2024/7/10.
//

#import "SLHomeMenuView.h"

static  NSString *const kCellId = @"SLHomeMenuCell";
@interface SLHomeMenuView ()<UITableViewDelegate,UITableViewDataSource>{
    BOOL _isLongPress;
}

@property (nonatomic, strong)NSMutableArray *dataArray;
@property (nonatomic, strong)UITableView *tableView;
@end

@implementation SLHomeMenuView
-(void)awakeFromNib{
    [super awakeFromNib];
    if (kScreenHeight <= 667) {
        _logoTopSpace.constant = 50;
        _menuBgTopSpace.constant = 50;
        _bottomToBottomSpace.constant = 0;
        _topViewTopSpace.constant = 30;
    }else if (kScreenHeight <= 736){
        _logoTopSpace.constant = 60;
        _menuBgTopSpace.constant = 60;
        _bottomToBottomSpace.constant = 10;
        _topViewTopSpace.constant = 40;
    }
    _nameBtn.layer.cornerRadius = 25;
    _nameBtn.layer.masksToBounds = YES;
    _nameBtn.btnStyle = 1;
    if (kScreenHeight <= 736) {
        [self.bgView1 addPortColorWithStart:kHexColor(@"#0C0C0D") endColor:kHexColor(@"#29292B") frame:CGRectMake(0, 0, self.bgView1.width,110)];
        [self.bgView2 addPortColorWithStart:kHexColor(@"#29292B") endColor:kHexColor(@"#18181A") frame:CGRectMake(0, 0, self.bgView2.width,self.bgView2.height)];
    }
    
    self.bgView.backgroundColor = kHexColor(@"18181A");
 
    _line.backgroundColor = kHexColor(@"#333333");
    _grayView.userInteractionEnabled = YES;
    kWeakSelf(self);
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        if (weakself.btnBlock) weakself.btnBlock(99);
    }];
    [_grayView addGestureRecognizer:tap];
    
    _supLbl.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        if (weakself.btnBlock) weakself.btnBlock(3);
    }];
    [_supLbl addGestureRecognizer:tap2];
    
    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
    swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
    [self addGestureRecognizer:swipeGesture];
    
    [self configDatas];
    
    UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress)];
    longTap.minimumPressDuration = 2;
    _logo.userInteractionEnabled = YES;
    [_logo addGestureRecognizer:longTap];
    
    [self.menuBgView addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
}

-(void)longPress{
    if (_isLongPress) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self->_isLongPress = NO;
        });
    }else{
        _isLongPress = YES;
        if (self.btnBlock) {
            self.btnBlock(100);
        }
    }
}

-(void)configDatas{
    [self.dataArray removeAllObjects];
//    NSArray *titles = @[kLocalString(@"Preferences"),kLocalString(@"Works with"),kLocalString(@"Buy a new lock"),kLocalString(@"About")];
    NSArray *titles = @[kLocalString(@"Add new device"),kLocalString(@"Preferences"),kLocalString(@"Change password"),kLocalString(@"Contact us"),kLocalString(@"Terms & conditions"),kLocalString(@"Sign out")];
    _menuBgViewHeight.constant = titles.count * 44;
    [titles enumerateObjectsUsingBlock:^(NSString *title, NSUInteger idx, BOOL * _Nonnull stop) {
        SLHomeMenuModel *model = [SLHomeMenuModel new];
        model.title = title;
        if ([title isEqualToString:kLocalString(@"Buy a new lock")]) {
            model.iconName = kLocalString(@"Coupon");
        }
        [_dataArray addObject:model];
    }];
}

- (void)handleSwipeGesture:(UISwipeGestureRecognizer *)recognizer {
    // 在这里添加您想要处理左滑手势的代码
    [UIView animateWithDuration:0.25 animations:^{
        self->_bgView.mj_x = -kScreenWidth;
        self.alpha = 0;
    }completion:^(BOOL finished) {
        self.hidden = YES;
    }];
}

- (IBAction)btnAction:(id)sender {
    if (sender == _editBtn) {
        if (_btnBlock) _btnBlock(1);
    }
    else if (sender == _nameBtn){
        if (_btnBlock) _btnBlock(2);
    }
    else if (sender == _shareBtn){
        if (_btnBlock) _btnBlock(3);
    }
    
}

-(void)refreshSubViews{
    if (!kUserInfo) {
        return;
    }
    self.nameLbl.text = kUserInfo.nickName;
    self.emailLbl.text = kUserInfo.email;
    if (kUserInfo.userPicture.length > 0) {
        [self.nameBtn setImageWithURL:[NSURL URLWithString:kUserInfo.userPicture] forState:UIControlStateNormal placeholder:kDefaultImage];
    }else{
        NSString *text = [kUserInfo.nickName substringWithRange:NSMakeRange(0, 1)];
        [self.nameBtn setTitle:text forState:UIControlStateNormal];
    }
    
    _versionLbl.text = [NSString stringWithFormat:@"Ver %@",kAppVersion];
}


#pragma mark - ================<tableViewDataSource>===============
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SLHomeMenuCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellId];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    SLHomeMenuModel *model = _dataArray[indexPath.row];
    cell.model = model;
    return cell;
}

#pragma mark - ================<UITableViewDelegate>===============
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UITableViewHeaderFooterView *headview = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"head"];
    if (!headview) {
        headview = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:@"head"];
        headview.frame = CGRectMake(0, 0, kScreenWidth, 0.1);
        headview.contentView.backgroundColor = KClearColor;
    }
    return headview;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UITableViewHeaderFooterView *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"foot"];
    if (!footerView) {
        footerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:@"foot"];
        footerView.frame = CGRectMake(0, 0, kScreenWidth, 0.1);
        footerView.contentView.backgroundColor = KClearColor;
    }
    return footerView;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    SLHomeMenuModel *model = _dataArray[indexPath.row];
    if (self.menuCellBlock) {
        self.menuCellBlock(model);
    }
}

#pragma mark - ================<懒加载>===============
-(UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.estimatedRowHeight = 44;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerNib:[UINib nibWithNibName:kCellId bundle:nil] forCellReuseIdentifier:kCellId];
        _tableView.backgroundColor = KClearColor;
        _tableView.scrollEnabled = NO;
    }
    return _tableView;
}

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

@end
